package com.thinkTank.oa.lucene;

import java.util.ArrayList;
import java.util.List;

import com.thinkTank.oa.dao.PersonMapper;



public class SolrOperation<T> implements IsolrOperation<T>{
	
	
	@Override
	public boolean add(String target,List<T> object) {
		return SolrUtils.add(target, object);
	}
	
	public boolean update(String target,T object){
		List<T> list =new ArrayList<T>();
		list.add(object);
		return SolrUtils.update(target, list);
	}
	public Pagination luceneSearch(int page,int size,Integer searchType,String keyWords){
		
		Pagination pa= null;
		if(searchType==1){
			if(keyWords.startsWith("E")){
				keyWords="expertNumber:\""+keyWords+"\"";
			}else{
				keyWords="person:"+keyWords;
			}
			pa =SolrUtils.search("person",keyWords,null,page,size);
		}
		return pa;
	}
	/*****
	 * @date 2018-09-25
	 * @author dq
	 * 根据参数查询lucene 给出结果集
	 * @param page 当前第几页 
	 * @param size 每页多少行
	 * @param matchType
	 * 匹配类型 
	 * 1 专家匹配需求 2 客户匹配需求 3客户匹配项目 4 项目匹配需求 5 项目匹配客户 6 需求匹配项目 7 需求匹配专家
	 * @param objectId 匹配的对象ID  例如: 专家匹配需求 就把专家ID传进来
	 * @param researchField 领域 比如 生物科学
	 * @param area 查 对象详情的时候 怎么显示的，就怎么传过来
	 * @param keywords 检索关键字
	 * @return
	 */
	 public Pagination queryList(int page,int size,Integer matchType,String objectId,String researchField,String area,String keywords){
		
			Pagination pa= null;
			switch(matchType){
			case 1://专家匹配需求 
				 pa = SolrUtils.search("person", "expertId:"+objectId, null, 1, 1);
				 LucenePerson peron = (LucenePerson)pa.getList().get(0);
				//条件拼接
				String mathObj = peron.getResear();
				String str =peron.getProfessionaField()=="null"?"":peron.getProfessionaField();
				if(str==null||str.equals("null")){
					str="";
				}
				if(str.length()>100){
					str= str.substring(0, 50);
				}
				if(mathObj.endsWith("其他")){
					mathObj="";
				}
				mathObj+=" "+str;
				//专家的领域和专业领域 匹配需求的 需求名称、需求内容、所属领域匹配
				String keyword = "demandName:"+mathObj +" OR "+"demandContent:"+mathObj+" OR "+"researchField:"+mathObj;
				
				if(null!=keywords&&!"".equals(keywords)){
					if(keywords.startsWith("D")){
						keyword="("+keyword+")"+" AND demandNumber:\""+keywords+"\"";
					}else{
						keyword="("+keyword+")"+" AND demand:"+keywords;
					}
					
				}
				if(null!=researchField&&!"".equals(researchField)){
					keyword="("+keyword+")"+" AND researchField:"+researchField;
				}
				if(null!=area&&!"".equals(area)){
					keyword="("+keyword+")"+" AND area:"+area;

				}
				
				try{
					pa =SolrUtils.search("demand",keyword,null,page,size);
				}catch(Exception e){
					return null;
				}
				
				break;
			case 2://2 客户匹配需求  客户的主要需求去匹配需求的需求类型、需求名称
				 pa = SolrUtils.search("customer", "customerId:"+objectId, null, 1, 1);
				 LuceneCustomer lc =(LuceneCustomer)pa.getList().get(0);
				 String mathobj1 =lc.getMajorNeeds();
				 String keyword1 ="demandType:"+mathobj1+ " OR "+"demandName:"+mathobj1;
				 
				 pa =SolrUtils.search("demand",keyword1,null,page,size);
				break;
			case 3://3客户匹配项目 客户跟项目的匹配也是  用客户的主要需求去匹配项目类型、项目名称
				 pa = SolrUtils.search("customer", "customerId:"+objectId, null, 1, 1);
				 LuceneCustomer lcc =(LuceneCustomer)pa.getList().get(0);
				 String mathobjc =lcc.getMajorNeeds();
				 String keyword2 ="projectType:"+mathobjc+ " OR "+"projectName:"+mathobjc;
				 pa =SolrUtils.search("project",keyword2,null,page,size);
				break;
			case 4://4 项目匹配需求 项目 VS 需求：项目名称、项目简介、项目关键字匹配需求名称、需求内容
				 pa = SolrUtils.search("project", "projectId:"+objectId, null, 1, 1);
				 LuceneProject pro =(LuceneProject)pa.getList().get(0);
				 String introduction="";
				 if(null!=pro.getIntroduction()){
					 if(pro.getIntroduction().length()>100){
						 introduction=pro.getIntroduction().substring(0, 50);
					 }else{
						 introduction=pro.getIntroduction();
					 }
				 }
				 String mathld3 = pro.getProjectName()+" " +introduction+" "+pro.getKeyWords();
				 String keyword5= "demandName:"+mathld3 +" OR "+"demandContent:"+mathld3;
				 if(null!=keywords&&!"".equals(keywords)){
						if(keywords.startsWith("D")){
							keyword5="("+keyword5+")"+" AND demandNumber:\""+keywords+"\"";
						}else{
							keyword5="("+keyword5+")"+" AND demand:"+keywords;
						}
						
					}
					if(null!=researchField&&!"".equals(researchField)){
						keyword5="("+keyword5+")"+" AND researchField:"+researchField;
					}
					if(null!=area&&!"".equals(area)){
						keyword5="("+keyword5+")"+" AND area:"+area;

					}
				 
				 pa =SolrUtils.search("demand",keyword5,null,page,size);
				break;
			case 5://5 项目匹配客户 项目 VS 客户：项目名称、项目简介、项目关键字、项目类型匹配客户的主要需求
				 pa = SolrUtils.search("project", "projectId:"+objectId, null, 1, 1);
				 LuceneProject pro1 =(LuceneProject)pa.getList().get(0);
				 String mathld4 = pro1.getProjectName()+" " +pro1.getIntroduction()+" "+pro1.getKeyWords()+" "+pro1.getProType();
				 String keyword6= "majorNeeds:"+mathld4 ;
				 pa =SolrUtils.search("customer",keyword6,null,page,size);
				break;
			case 6:// 6 需求匹配项目 需求名称、需求内容匹配项目名称、项目简介、项目关键字
				 pa = SolrUtils.search("demand", "demandId:"+objectId, null, 1, 1);
				 LuceneDemand ld =(LuceneDemand)pa.getList().get(0);
				 String mathld1 = ld.getDemandName()+" "+ld.getDemandContent();
				 
				 if(mathld1.length()>80){
					 mathld1= mathld1.substring(0, 80);
				 }
				 String keyword4= "projectName:"+mathld1 +" OR "+"introduction:"+mathld1+" OR "+"keyWords:"+mathld1;
				
				 if(null!=keywords&&!"".equals(keywords)){
						if(keywords.startsWith("R")){
							keyword4="("+keyword4+")"+" AND projectNumber:\""+keywords+"\"";
						}else{
							keyword4="("+keyword4+")"+" AND project:"+keywords;
						}
						
					}
					if(null!=researchField&&!"".equals(researchField)){
						keyword4="("+keyword4+")"+" AND researchField:"+researchField;
					}
					if(null!=area&&!"".equals(area)){
						keyword4="("+keyword4+")"+" AND area:"+area;

					}
				 
				 
				 
				 
				 pa =SolrUtils.search("project",keyword4,null,page,size);
				break;
			case 7://7 需求匹配专家  //需求名称、需求内容匹配专家的所属领域、专业领域、人才级别
				 pa = SolrUtils.search("demand", "demandId:"+objectId, null, 1, 1);
				 LuceneDemand ld1 =(LuceneDemand)pa.getList().get(0);
				 String mathld = ld1.getDemandName()+" "+ld1.getDemandContent();
				 if(mathld.length()>80){
					 mathld= mathld.substring(0, 80);
				 }
				 String keyword3= "researchField:"+mathld +" OR "+"talentLevel:"+mathld+" OR "+"professionaField:"+mathld;
				
				 
				 if(null!=keywords&&!"".equals(keywords)){
						if(keywords.startsWith("E")){
							keyword3="("+keyword3+")"+" AND expertNumber:\""+keywords+"\"";
						}else{
							keyword3="("+keyword3+")"+" AND person:"+keywords;
						}
						
					}
					if(null!=researchField&&!"".equals(researchField)){
						keyword3="("+keyword3+")"+" AND researchField:"+researchField;
					}
					if(null!=area&&!"".equals(area)){
						keyword3="("+keyword3+")"+" AND area:"+area;

					}
				 pa =SolrUtils.search("person",keyword3,null,page,size);
				break;
			
			}
			return pa;
		}
	/***
	 * @author dq
	 * @date 2018-09-20
	 * 自动匹配功能
	 * @param matchType 匹配类型 
	 * 1 专家匹配需求 2 客户匹配需求 3客户匹配项目 4 项目匹配需求 5 项目匹配客户 6 需求匹配项目 7 需求匹配专家
	 * @param objectId 匹配的对象ID  例如: 专家匹配需求 就把专家ID传进来
	 * @return
	 */
	@Override
	public Pagination queryList(int page,int size,Integer matchType,String objectId){
		Pagination pa= null;
		switch(matchType){
		case 1://专家匹配需求 
			 pa = SolrUtils.search("person", "expertId:"+objectId, null, 1, 1);
			LucenePerson peron = (LucenePerson)pa.getList().get(0);
			//条件拼接
			String mathObj = peron.getResear();
			String str =peron.getProfessionaField()=="null"?"":peron.getProfessionaField();
			if(str==null){
				str="";
			}
			if(str.length()>100){
				str= str.substring(0, 50);
			}
			mathObj+=" "+str;
			//专家的领域和专业领域 匹配需求的 需求名称、需求内容、所属领域匹配
			String keyword = "demandName:"+mathObj +" OR "+"demandContent:"+mathObj+" OR "+"researchField:"+mathObj;
			 pa =SolrUtils.search("demand",keyword,null,page,size);
			break;
		case 2://2 客户匹配需求  客户的主要需求去匹配需求的需求类型、需求名称
			 pa = SolrUtils.search("customer", "customerId:"+objectId, null, 1, 1);
			 LuceneCustomer lc =(LuceneCustomer)pa.getList().get(0);
			 String mathobj1 =lc.getMajorNeeds();
			 String keyword1 ="demandType:"+mathobj1+ " OR "+"demandName:"+mathobj1;
			 pa =SolrUtils.search("demand",keyword1,null,page,size);
			break;
		case 3://3客户匹配项目 客户跟项目的匹配也是  用客户的主要需求去匹配项目类型、项目名称
			 pa = SolrUtils.search("customer", "customerId:"+objectId, null, 1, 1);
			 LuceneCustomer lcc =(LuceneCustomer)pa.getList().get(0);
			 String mathobjc =lcc.getMajorNeeds();
			 String keyword2 ="projectType:"+mathobjc+ " OR "+"projectName:"+mathobjc;
			 pa =SolrUtils.search("project",keyword2,null,page,size);
			break;
		case 4://4 项目匹配需求 项目 VS 需求：项目名称、项目简介、项目关键字匹配需求名称、需求内容
			 pa = SolrUtils.search("project", "projectId:"+objectId, null, 1, 1);
			 LuceneProject pro =(LuceneProject)pa.getList().get(0);
			 
			 String introduction="";
			 if(null!=pro.getIntroduction()){
				 if(pro.getIntroduction().length()>100){
					 introduction=pro.getIntroduction().substring(0, 50);
				 }else{
					 introduction=pro.getIntroduction();
				 }
			 }
			 String mathld3 = pro.getProjectName()+" " +introduction+" "+pro.getKeyWords();
			 String keyword5= "demandName:"+mathld3 +" OR "+"demandContent:"+mathld3;
			 pa =SolrUtils.search("demand",keyword5,null,page,size);
			break;
		case 5://5 项目匹配客户 项目 VS 客户：项目名称、项目简介、项目关键字、项目类型匹配客户的主要需求
			 pa = SolrUtils.search("project", "projectId:"+objectId, null, 1, 1);
			 LuceneProject pro1 =(LuceneProject)pa.getList().get(0);
			 String mathld4 = pro1.getProjectName()+" " +pro1.getIntroduction()+" "+pro1.getKeyWords()+" "+pro1.getProType();
			 String keyword6= "majorNeeds:"+mathld4 ;
			 pa =SolrUtils.search("customer",keyword6,null,page,size);
			break;
		case 6:// 6 需求匹配项目 需求名称、需求内容匹配项目名称、项目简介、项目关键字
			 pa = SolrUtils.search("demand", "demandId:"+objectId, null, 1, 1);
			 LuceneDemand ld =(LuceneDemand)pa.getList().get(0);
			 String mathld1 = ld.getDemandName()+" "+ld.getDemandContent();
			 if(mathld1.length()>80){
				 mathld1= mathld1.substring(0, 80);
			 }
			 String keyword4= "projectName:"+mathld1 +" OR "+"introduction:"+mathld1+" OR "+"keyWords:"+mathld1;
			 pa =SolrUtils.search("project",keyword4,null,page,size);
			break;
		case 7://7 需求匹配专家  //需求名称、需求内容匹配专家的所属领域、专业领域、人才级别
			 pa = SolrUtils.search("demand", "demandId:"+objectId, null, 1, 1);
			 LuceneDemand ld1 =(LuceneDemand)pa.getList().get(0);
			 String mathld = ld1.getDemandName()+" "+ld1.getDemandContent();
			 if(mathld.length()>80){
				 mathld= mathld.substring(0, 80);
			 }
			 String keyword3= "researchField:"+mathld +" OR "+"talentLevel:"+mathld+" OR "+"professionaField:"+mathld;
			 pa =SolrUtils.search("person",keyword3,null,page,size);
			break;
		
		}
		return pa;
	}

}
